﻿using Microsoft.Office.Interop.Excel;
using Microsoft.Reporting.Map.WebForms.BingMaps;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using ZXing.QrCode.Internal;

namespace CardMonitor.Utils
{
    internal class NewIDcardread
    {
       
        [DllImport("dll/Sdtapi.dll")]
        private static extern int InitComm(int iPort);
        [DllImport("dll/Sdtapi.dll")]
        private static extern int Authenticate();
        [DllImport("dll/Sdtapi.dll")]
        private static extern int ReadBaseInfos(StringBuilder Name, StringBuilder Gender, StringBuilder Folk,
                                                    StringBuilder BirthDay, StringBuilder Code, StringBuilder Address,
                                                        StringBuilder Agency, StringBuilder ExpireStart, StringBuilder ExpireEnd);
        [DllImport("dll/Sdtapi.dll")]
        private static extern int CloseComm();
        [DllImport("dll/Sdtapi.dll")]
        private static extern int ReadBaseMsg(byte[] pMsg, ref int len);
        [DllImport("dll/sdtapi.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern int ReadIINSNDN(StringBuilder pMsg);//读物理卡号
        [DllImport("dll/Sdtapi.dll")]
        private static extern int ReadBaseMsgW(byte[] pMsg, ref int len);
        public  static int Opendevice()
        {
            //打开端口
            int intOpenRet = InitComm(1001);//1成功
            return intOpenRet;

        }
        public static int CardAuthentication()//读卡认证
        {
            int intReadRet = Authenticate();
            return intReadRet;
         
        }
        public  static NewCardInfo ReadIDcard()//读身份证
        {
            NewCardInfo newCard = null;
            StringBuilder Name = new StringBuilder(31);
            StringBuilder Gender = new StringBuilder(3);
            StringBuilder Folk = new StringBuilder(10);
            StringBuilder BirthDay = new StringBuilder(9);
            StringBuilder Code = new StringBuilder(19);
            StringBuilder Address = new StringBuilder(71);
            StringBuilder Agency = new StringBuilder(31);
            StringBuilder ExpireStart = new StringBuilder(9);
            StringBuilder ExpireEnd = new StringBuilder(9);
            StringBuilder driverMacCard = new StringBuilder(16);
            byte[] pMsg = new byte[256];
            int len = 0;
            string[] temp;
            string[] baseinfo = new string[9];
            char[] param = { '\0' };
            int intReadBaseInfosRet = ReadBaseInfos(Name, Gender, Folk, BirthDay, Code, Address, Agency, ExpireStart, ExpireEnd);
            if (intReadBaseInfosRet != 1)
            {
                MessageBox.Show("读卡失败");
               // CloseComm();
                return newCard;
            }
            else
            {
                newCard = new NewCardInfo();
                newCard.CardNo= Code.ToString();
                newCard.Name= Name.ToString();
                int code = ReadIINSNDN(driverMacCard);
                newCard.driverMacCard = driverMacCard.ToString();
                return newCard;
            }
        }
        public void closeID()
        {
            CloseComm();
        }

     }
    public class NewCardInfo
    {
        /// <summary>
        /// 身份证号码
        /// </summary>
        public string CardNo;
        /// <summary>
        /// 身份证物理卡号
        /// </summary>
        public string driverMacCard;
        /// <summary>
        /// 姓名
        /// </summary>
        public string Name;
        /// <summary>
        /// 性别
        /// </summary>
        public string Sex;
        /// <summary>
        /// 出生日期
        /// </summary>
        public string Birthday;
        /// <summary>
        /// 地址
        /// </summary>
        public string Address;
        /// <summary>
        /// 追加地址
        /// </summary>
        public string AddressEx;
        /// <summary>
        /// 发卡机关
        /// </summary>
        public string Department;
        /// <summary>
        /// 证件开始日期
        /// </summary>
        public string StartDate;
        /// <summary>
        /// 证件结束日期
        /// </summary>
        public string EndDate;
        /// <summary>
        /// 民族
        /// </summary>
        public string Nation;
        /// <summary>
        /// 相片路径
        /// </summary>
        public string PhotoPath;
        /// <summary>
        /// 相片的字节信息
        /// </summary>
        public byte[] ArrPhotoByte;
        /// <summary>
        /// 时时图片字节信息
        /// </summary>
        public byte[] PhoTimeByte;
    }
}
